/**
 * 
 */
package com.li.service;

import java.util.Date;
import java.util.Map;

import com.li.message.req.ReqBaseMessage;
import com.li.message.req.ReqEventMessage;
import com.li.message.req.ReqImageMessage;
import com.li.message.req.ReqLinkMessage;
import com.li.message.req.ReqLocationMessage;
import com.li.message.req.ReqTextMessage;
import com.li.message.req.ReqVoiceMessage;
import com.li.utils.MessageUtil;

/**
 * 用来封装接收到的消息
 * @author liandyao
 * @date 2016-1-23
 */
public class ManagerReqMsg {

	public static ReqBaseMessage getBaseMessage(Map<String, String> requestMap){
		ReqBaseMessage bm = new ReqTextMessage() ;
		// 发送方帐号（open_id）  
        String fromUserName = requestMap.get("FromUserName");  
        // 公众帐号  
        String toUserName = requestMap.get("ToUserName"); 
        // 消息类型  
        String msgType = requestMap.get("MsgType");   
        // 文本消息
		if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
			//respContent = "您发送的是文本消息！";
			bm = new ReqTextMessage() ;
			 
		}
		//图片信息
		else if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)){
			bm = new ReqImageMessage() ;
			 
		}
		// 地理位置消息  
        else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) {  
        	bm = new ReqLocationMessage() ;
        	 
        }  
        // 链接消息  
        else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) {  
            bm = new ReqLinkMessage() ;
        }  
        // 音频消息  
        else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {  
            bm = new ReqVoiceMessage() ;
        }  
        // 事件推送  
        else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {  
           
            
            bm = new ReqEventMessage() ;
            String event = requestMap.get("Event");
            ((ReqEventMessage)bm).setEvent(event);
            
            String eventKey = requestMap.get("EventKey");
            ((ReqEventMessage)bm).setEventKey(eventKey);
            
            /*
             *  // 事件类型  
             * String eventType = requestMap.get("Event");  
            
            // 订阅  
            if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {  
                respContent = "谢谢您的关注！";  
            }  
            // 取消订阅  
            else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {  
                // TODO 取消订阅后用户再收不到公众号发送的消息，因此不需要回复消息  
            }  
            // 自定义菜单点击事件  
            else if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)) {  
                // 事件KEY值，与创建自定义菜单时指定的KEY值对应  
                String eventKey = requestMap.get("EventKey");  

                if (eventKey.equals("11")) {  
                    respContent = "天气预报菜单项被点击！";  
                } else if (eventKey.equals("12")) {  
                    respContent = "公交查询菜单项被点击！";  
                } else if (eventKey.equals("13")) {  
                    respContent = "周边搜索菜单项被点击！";  
                } else if (eventKey.equals("14")) {  
                    respContent = "历史上的今天菜单项被点击！";  
                } else if (eventKey.equals("21")) {  
                    respContent = "歌曲点播菜单项被点击！";  
                } else if (eventKey.equals("22")) {  
                    respContent = "经典游戏菜单项被点击！";  
                } else if (eventKey.equals("23")) {  
                    respContent = "美女电台菜单项被点击！";  
                } else if (eventKey.equals("24")) {  
                    respContent = "人脸识别菜单项被点击！";  
                } else if (eventKey.equals("25")) {  
                    respContent = "聊天唠嗑菜单项被点击！";  
                } else if (eventKey.equals("31")) {  
                    respContent = "Q友圈菜单项被点击！";  
                } else if (eventKey.equals("32")) {  
                    respContent = "电影排行榜菜单项被点击！";  
                } else if (eventKey.equals("33")) {  
                    respContent = "幽默笑话菜单项被点击！";  
                }  
            }  */
        }  
		
		else{
			//如果不知道是什么类型，按照文本类型处理
			bm = new ReqTextMessage() ;
		}
		try {
			MessageUtil.reflectMessage(bm, requestMap) ;
		} catch (Exception e) { 
			e.printStackTrace();
		}
		//当前服务器时间
		bm.setCreateTime(new Date().getTime()) ;
		bm.setFromUserName(fromUserName) ;
		bm.setToUserName(toUserName);
		bm.setMsgType(msgType) ;
		return bm ;
	}
}
